#include"ConfigOperate.h"


//string ConfigOperate::k_class = string("Class");
//string ConfigOperate::k_fun = string("Function");
//string ConfigOperate::k_key = string("Key");



/*ConfigOperate::ConfigOperate()
{
	k_class = "Class";
	k_fun = "Function";
	k_key = "Key";
}
*/
void ConfigOperate::SetFilePath(string &filepath)
{
	k_filepath = filepath;
}

bool ConfigOperate::OpenFile()
{
	if(k_fin.is_open() == true)
		k_fin.close();
	k_fin.open(k_filepath.c_str());
	if(k_fin == NULL)
	{
		k_error = "Can't open file " + k_filepath;
		return false;
	}
	return true;
}

bool ConfigOperate::FindName()
{
	if(k_str.find('[') != -1)
	{
		k_name = k_str.substr(k_str.find('[')+1, k_str.find(']') - k_str.find('[') - 1);
		return true;
	}
	else
		return false;
}

bool ConfigOperate::FindValue()
{
	if(k_str.find('=') == -1)
		return false;
	k_value = k_str.substr(0, k_str.find('='));
	return true;
}


bool ConfigOperate::ReadKeyFile(KeyOperate *k_operate)
{
	unsigned int classId = 0;
	unsigned int funId = 0;
	unsigned int keyId = 0;
	while(getline(k_fin, k_str))
	{
		FindName();
		if(k_name == k_class)
		{
			funId = 0;
			classId = k_operate->addClass();
			printf("CLASS: %d\n",classId);
		}
		if(k_name == k_fun)
		{
			keyId = 0;
			funId = k_operate->addFun(classId);
			printf("    FUNCTION: %d\n",funId);
		}
		if(k_name == k_key)
		{
			string str;
			getline(k_fin, str);
			if(str != "[Key]")
			{
				keyId = k_operate->addK(classId, funId, str);
				printf("        KEY: %d\n",keyId);
				printf("%s\n", str.c_str());
			}
		}
	}
	return true;
}

/*
unsigned int ConfigOperate::WriteFile()
{

}
*/


bool ConfigOperate::ReadAlgConf(AlgComInfo * algcominfo, unsigned int funid)
{
	int total = 0;
	int i = 0;
//	AlgComInfo algcominfo;
	while(getline(k_fin, k_str))
	{
		FindName();
		if(k_name == "main" && k_str != "[main]")
		{
			if(FindValue())
			{
				if(k_value == "ClassID")
				{
					string s = k_str.substr(k_str.find('=')+1);
					algcominfo->classID = stoi(s);
				}
				if(k_value == "total")
					total = stoi(k_str.substr(k_str.find('=')+1));
			}
			else
				continue;
		}
		else if(k_name == "function" && k_str != "[function]")
		{
			if(FindValue())
			{
				if(ValueAnalyse(algcominfo, funid))
					return true;
				else
					continue;
			}
			else
				continue;
		}
	}
	return false;
}

bool ConfigOperate::ValueAnalyse(AlgComInfo * algcominfo, unsigned int funid)
{
	if(algcominfo->classID == 0)
	{
		if(k_value == "functionID")
		{
			algcominfo->funID = stoi(k_str.substr(k_str.find('=')+1));
			if(algcominfo->funID != funid)
				return false;
		}
		while(getline(k_fin, k_str))
		{
			FindValue();
			if(k_value == "algName")
				algcominfo->algName = k_str.substr(k_str.find('=')+1);
			if(k_value == "dllAdress")
				algcominfo->dllAdress = k_str.substr(k_str.find('=')+1);
			if(k_value == "k_iv")
			{
				algcominfo->k_iv = stoi(k_str.substr(k_str.find('=')+1));
				return true;
			}
			else
				continue;
		}
	}

	else if(algcominfo->classID == 1)
	{
		if(k_value == "functionID")
		{
			algcominfo->funID = stoi(k_str.substr(k_str.find('=')+1));
			if(algcominfo->funID != funid)
				return false;
		}
		while(getline(k_fin, k_str))
		{
			FindValue();
			if(k_value == "algName")
				algcominfo->algName = k_str.substr(k_str.find('=')+1);
			if(k_value == "dllAdress")
				algcominfo->dllAdress = k_str.substr(k_str.find('=')+1);
			if(k_value == "k_block")
				algcominfo->k_block= stoi(k_str.substr(k_str.find('=')+1));
			if(k_value == "k_iv")
			{
				algcominfo->k_iv = stoi(k_str.substr(k_str.find('=')+1));
				return true;
			}
			else
				continue;
		}
	}

	else if(algcominfo->classID == 2)
	{
		if(k_value == "functionID")
		{
			algcominfo->funID = stoi(k_str.substr(k_str.find('=')+1));
			if(algcominfo->funID != funid)
				return false;
		}
		while(getline(k_fin, k_str))
		{
			FindValue();
			if(k_value == "algName")
				algcominfo->algName = k_str.substr(k_str.find('=')+1);
			if(k_value == "dllAdress")
				algcominfo->dllAdress = k_str.substr(k_str.find('=')+1);
			if(k_value == "k_RSA_N")
				algcominfo->k_RSA_N = stoi(k_str.substr(k_str.find('=')+1));
			if(k_value == "k_RSA_e")
				algcominfo->k_RSA_e = stoi(k_str.substr(k_str.find('=')+1));
			if(k_value == "k_EIGamal_y")
				algcominfo->k_EIGamal_y = stoi(k_str.substr(k_str.find('=')+1));
			if(k_value == "k_ECC_Q")
				algcominfo->k_ECC_Q = stoi(k_str.substr(k_str.find('=')+1));
			if(k_value == "k_SM2_Q")
				algcominfo->k_SM2_Q = stoi(k_str.substr(k_str.find('=')+1));
			if(k_value == "k_private")
				algcominfo->k_private = stoi(k_str.substr(k_str.find('=')+1));
			if(k_value == "k_random")
			{
				algcominfo->k_random = stoi(k_str.substr(k_str.find('=')+1));
				return true;
			}
			else
				continue;			
		}
	}
	
	else if(algcominfo->classID == 4)
	{
		if(k_value == "functionID")
		{
			algcominfo->funID = stoi(k_str.substr(k_str.find('=')+1));
			if(algcominfo->funID != funid)
				return false;
		}
		while(getline(k_fin, k_str))
		{
			FindValue();
			if(k_value == "algName")
				algcominfo->algName = k_str.substr(k_str.find('=')+1);
			if(k_value == "dllAdress")
			{
				algcominfo->dllAdress = k_str.substr(k_str.find('=')+1);
				return true;
			}
			else
				continue;
		}
	}
	return false;
}